package mx.com.matersys.vacs.service.usuarios;

import java.util.ArrayList;
import java.util.Collection;

import mx.com.matersys.vacs.domain.CustomUserDetails;
import mx.com.matersys.vacs.domain.UserDetailsImpl;
import mx.com.matersys.vacs.domain.UserVacs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("assembler")
public class UserAssembler {
	

	protected static final Logger log = LoggerFactory.getLogger(UserAssembler.class);

	@Transactional(readOnly = true)
	public CustomUserDetails buildUserFromUserEntity(UserVacs userEntity) {

		CustomUserDetails customUserDetails;
		Collection<GrantedAuthority> authorities;
		String name;
		
		// por el momento solo tenemos un rol para cada usuario
		authorities = new ArrayList<GrantedAuthority>();
		authorities.add(new GrantedAuthorityImpl(userEntity.getIdrol().getTipoRol()));

		name = userEntity.getNombre() + " " + userEntity.getAppaterno();
		
		// armamos el CustomUserDetails
		customUserDetails = new CustomUserDetails(name, authorities,
				userEntity.getPasswd(), userEntity, userEntity.getUsername());

		log.info("User Assembler " + customUserDetails);
		return customUserDetails;
	}
	
	public UserDetailsImpl buildUserFromUserVacs(UserVacs userEntity) {
		
		Collection<GrantedAuthority> authorities;
		UserDetailsImpl userDetails;
		
		authorities = new ArrayList<GrantedAuthority>();
		authorities.add(new GrantedAuthorityImpl(userEntity.getIdrol().getTipoRol()));
		
		
		userDetails = new UserDetailsImpl(userEntity.getUsername(), userEntity.getPasswd(), authorities, userEntity);

		return userDetails;
	}

}
